﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbolandiaBL
{

    internal abstract class ProductsBaseProvider : ErrorExceptionBase
    {
        protected ArbolandiaDBDataContext _db;
        protected ObSession _obSession;
        protected int _productId;
        private ImageProvider _iprov;

        protected ProductsBaseProvider(ObSession obSession, int productId)
        {
            _db = new ArbolandiaDBDataContext();
            _obSession = obSession;
            _productId = productId;
            _iprov = new ImageProvider();
        }
        protected ProductsBaseProvider()
        {
            _db = new ArbolandiaDBDataContext();
            _iprov = new ImageProvider();
        }
        internal virtual void InsertUpdateProduct() { }

        #region ProductDetail
        internal virtual void DeleteProductDetail(int id, int productid)
        {
            var query = _db.Product_ProductDetail.Where(w => w.Id == id && w.Product_ProductId == productid);
            if (query.Count() > 0) { _db.Product_ProductDetail.DeleteOnSubmit(query.First()); _db.SubmitChanges(); }
        }
        internal virtual void InsertUpdateProductDetail(Product_ProductDetail productDetail)
        {
            var query = _db.Product_ProductDetail.Where(w => w.Id == productDetail.Id);
            if (query.Count() > 0)
            {
                query.First().CodigoProducto = productDetail.CodigoProducto;
                query.First().Descripcion = productDetail.Descripcion;
                query.First().IsAcompanamiento = productDetail.IsAcompanamiento;
                query.First().Precio = productDetail.Precio;
                query.First().PrecioPromocion = productDetail.PrecioPromocion;
                query.First().Stock = productDetail.Stock;
            }
            else
            {
                _db.Product_ProductDetail.InsertOnSubmit(productDetail);
            }
            _db.SubmitChanges();
        }
        internal virtual List<Product_ProductDetail> GetProductDetailAll(int id)
        {
            var query = _db.Product_ProductDetail.Where(w => w.Product_ProductId == id);
            return query.Count() > 0 ? query.ToList() : new List<Product_ProductDetail>();
        }
        #endregion ProductDetail

        #region ProductPictures
        internal virtual List<object> GetProductPictures(int id)
        {
            var res = new List<object>();
            var query = from b in _db.spGetPicturesProduct(id)
                        select new
                        {
                            Id = b.Id,
                            url = b.url,
                            iorder = b.iorder
                        };
            foreach (var item in query)
            {
                res.Add(new
                {
                    Id = item.Id,
                    url = item.url,
                    iorder = item.iorder
                });
            }
            return res;

        }
        internal virtual void DeleteProductPicture(int id)
        {
            var query = _db.Product_ProductPictures.Where(w => w.Id == id);
            if (query.Count() > 0)
            {

                _db.Product_ProductPictures.DeleteOnSubmit(query.First());
                var query2 = _db.Picture.Where(w => w.GuidId == query.First().PictureThumbnailGuidId || w.GuidId == query.First().PictureFullSizeGuidId);
                _db.Picture.DeleteAllOnSubmit(query2.ToList());
                _db.SubmitChanges();
            }
        }
        internal virtual void InsertProductPicture(Picture small, Picture big, Product_ProductPictures pic)
        {

            _iprov.InsertUpdateImage(small);
            _iprov.InsertUpdateImage(big);

            pic.PictureFullSizeGuidId = big.GuidId;
            pic.PictureThumbnailGuidId = small.GuidId;

            var query = _db.Product_ProductPictures.Where(w =>
                w.Id == pic.Id &&
               w.Product_ProductId == pic.Product_ProductId);
            if (query.Count() <= 0)
            {
                _db.Product_ProductPictures.InsertOnSubmit(pic);
            }
            else
            {
                query.First().Order = pic.Order;
                query.First().PictureFullSizeGuidId = pic.PictureFullSizeGuidId;
                query.First().PictureThumbnailGuidId = pic.PictureThumbnailGuidId;
            }
            _db.SubmitChanges();

        }
        internal virtual Int32 GetCountImagesProduct(Int32 productid)
        {
            Int32 res = 0;
            var query = _db.Product_ProductPictures.Where(w => w.Product_ProductId == productid);
            res = query.Count();
            return res;
        }
        #endregion ProductPictures

        #region ProductComplements
        internal virtual void InsertUpdateComplements(Product_ProductComplement productComplement)
        {
            var query = _db.Product_ProductComplement.Where(w =>
                w.Product_ProductId == productComplement.Product_ProductId &&
                w.Product_ProductIdRef == productComplement.Product_ProductIdRef);
            if (query.Count() <= 0)
            {
                _db.Product_ProductComplement.InsertOnSubmit(productComplement);
                _db.SubmitChanges();
            }
        }
        internal virtual void DeleteComplements(int id)
        {
            var query = _db.Product_ProductComplement.Where(w => w.Id == id);
            if (query.Count() > 0) { _db.Product_ProductComplement.DeleteOnSubmit(query.First()); _db.SubmitChanges(); }
        }
        internal virtual List<object> GetComplements(Int32 id, bool isvarious)
        {
            var res = new List<object>();
            var query = from b in _db.spGetProductComplements(id, isvarious)
                        select new
                        {
                            Id = b.Id,
                            IdRef = b.IdRef,
                            Name = b.Name,
                            url = b.url
                        };
            foreach (var item in query)
            {
                res.Add(new
                {
                    Id = item.Id,
                    IdRef = item.IdRef,
                    Name = item.Name,
                    url = item.url
                });
            }
            return res;
        }
        #endregion ProductComplements

        #region DeleteProduct
        internal virtual void DeleteProduct() { }
        internal virtual void DeleteStep1() { }
        internal virtual void DeleteStep2()
        {
            var queryDetail = _db.Product_ProductDetail.Where(w => w.Product_ProductId == _productId);
            if (queryDetail.Count() > 0)
            {
                _db.Product_ProductDetail.DeleteAllOnSubmit(queryDetail.ToList());
                _db.SubmitChanges();
            }
        }
        internal virtual void DeleteStep3()
        {
            var querySubcategory = _db.Product_SubCategoryGroup.Where(w => w.Product_ProductId == _productId);
            if (querySubcategory.Count() > 0)
            {
                _db.Product_SubCategoryGroup.DeleteAllOnSubmit(querySubcategory.ToList());
                _db.SubmitChanges();
            }
        }
        internal virtual void DeleteStep4()
        {
            var queryProductPictures = _db.Product_ProductPictures.Where(w => w.Product_ProductId == _productId);
            if (queryProductPictures.Count() > 0)
            {
                _db.Product_ProductPictures.DeleteAllOnSubmit(queryProductPictures.ToList());
                _db.SubmitChanges();
            }
        }
        internal virtual void DeleteStep5()
        {
            var queryProductPreference = _db.Product_ProductPreferenceGroup.Where(w => w.Product_ProductId == _productId);
            if (queryProductPreference.Count() > 0)
            {
                _db.Product_ProductPreferenceGroup.DeleteAllOnSubmit(queryProductPreference.ToList());
                _db.SubmitChanges();
            }
        }
        internal virtual void DeleteStep6()
        {
            var queryProductComplement = _db.Product_ProductComplement.Where(w => w.Product_ProductId == _productId);
            if (queryProductComplement.Count() > 0)
            {
                _db.Product_ProductComplement.DeleteAllOnSubmit(queryProductComplement.ToList());
                _db.SubmitChanges();
            }
        }
        #endregion DeleteProduct
    }
}
